package com.bayihouse.controller;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bayihouse.common.*;
import com.bayihouse.excel.listener.ExcelOccupantListener;
import com.bayihouse.excel.listener.ExcelRoomListener;
import com.bayihouse.exception.BusinessException;
import com.bayihouse.model.entity.FileInfo;
import com.bayihouse.model.entity.Netobject;
import com.bayihouse.model.excel.ExcelOccupant;
import com.bayihouse.model.excel.ExcelRoom;
import com.bayihouse.model.request.file.FileSearchRequest;
import com.bayihouse.model.vo.FileVO;
import com.bayihouse.service.*;
import com.bayihouse.utils.ExcelCommunityUtils;
import com.bayihouse.utils.MinioUtils;
import io.minio.GetObjectResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 文件的上传和下载
 *
 * @author 罗家龙
 */
@Controller
@RequestMapping("/file")
@Slf4j
public class FileController {


    @Resource
    private NetObjectService netObjectService;


    @Resource
    private UnitService unitService;


    @Resource
    private HouseService houseService;


    @Resource
    private CommunityService communityService;

    @Resource
    private AdministerService userService;


    @Resource
    private FileInfoService fileInfoService;

    @Resource
    private MinioUtils minioService;

    @Resource
    private OccupantService occupantService;


    @Resource
    private RoomService roomService;

    @Resource
    private ExcelCommunityUtils excelCommunityUtils;


    @PostMapping("/page")
    @ResponseBody
    public BaseResponse<Page<FileInfo>> getFileListPage(@RequestBody FileSearchRequest fileSearchRequest) {
        if (fileSearchRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long current = fileSearchRequest.getCurrent();
        long size = fileSearchRequest.getPageSize();
        Page<FileInfo> fileInfoPage = fileInfoService.page(new Page<>(current, size),
                fileInfoService.getQueryWrapper(fileSearchRequest));
        return ResultUtils.success(fileInfoPage);
    }


    @PostMapping("/delete")
    @ResponseBody
    public BaseResponse<Boolean> deleteFile(DeleteRequest deleteRequest) throws Exception {
        if (deleteRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数不能为空");
        }
        if (deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "文件id不合法");
        }
        FileInfo fileInfo = fileInfoService.getById(deleteRequest.getId());
        boolean b = minioService.deleteObject(fileInfo.getFileName());
        if (!b) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "文件删除失败");
        }
        return ResultUtils.success(fileInfoService.removeById(deleteRequest.getId()));
    }


    /**
     * @param fileRequest 文件查看请求
     * @param response    想前端传输要查看的文件
     * @功能描述 下载文件:
     */
    @GetMapping("/watch")
    public void download(CommonRequest fileRequest, HttpServletResponse response) {
        if (fileRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        FileInfo fileInfoServiceById = fileInfoService.getById(fileRequest.getId());
        fileInfoService.download(fileInfoServiceById, response);
    }


    //upload
    @PostMapping("/upload")
    @ResponseBody
    public BaseResponse<FileVO> upload(@RequestPart("multipartFile") MultipartFile multipartFile, String fileParentPath, String detail, String type, HttpServletRequest request) throws Exception {
        System.out.println(fileParentPath);
        System.out.println(multipartFile.getOriginalFilename());
        if (multipartFile.isEmpty()) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "文件上传失败,网络问题");
        }
        boolean exist = minioService.isExist("bayihouse");
        if (exist) {
            Long id = userService.getLoginAdminister(request).getId();
            String res = minioService.uploadObject(multipartFile, id, fileParentPath);
            if (minioService.isObjectExist(res)) {
                FileVO fileVO = fileInfoService.upload(multipartFile, fileParentPath, detail, type, request);
                return ResultUtils.success(fileVO);
            } else {
                throw new BusinessException(ErrorCode.OPERATION_ERROR, "文件上传失败,网络问题");
            }
        } else {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "文件上传失败,内部错误");
        }
    }




    @PostMapping("/goToDataBase")
    public BaseResponse<Boolean> goToDataBase(Long id) throws Exception {
        FileInfo fileInfo = fileInfoService.getById(id);
        GetObjectResponse getObjectResponse = minioService.getObject(fileInfo.getFileName());
        Set<Netobject> netobjectSet = new HashSet<>();
        ExcelRoomListener excelRoomListener = new ExcelRoomListener(excelCommunityUtils);
        excelRoomListener.setNetobjectSet(netobjectSet);
        EasyExcel.read(getObjectResponse, ExcelRoom.class, excelRoomListener).sheet("房").doRead();
        ExcelOccupantListener excelOccupantListener = new ExcelOccupantListener(excelCommunityUtils);
        excelOccupantListener.setNetobjectSet(netobjectSet);
        GetObjectResponse getObjectResponse2 = minioService.getObject(fileInfo.getFileName());
        EasyExcel.read(getObjectResponse2, ExcelOccupant.class, excelOccupantListener).sheet("人").doRead();
        return ResultUtils.success(true);
    }


}
